﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace eJack.Utility
{
	public class CxMortgage
	{
		public static float CaculateMonthlyPayment(float floan, float frate, float fterm, out string serror)
		{
			float fPmt = 0.0f;
			serror = string.Empty;

			try
			{
				var fRate1 = frate / (12.0f * 100); // Get monthly and convert to decimal quivalent
				var fTerm1 = fterm * 12.0f; // Convert to months

				fPmt = (float)(floan * fRate1 / (1.0f - Math.Pow((1.0f + fRate1), -1 * fTerm1)));
			}
			catch (Exception ex)
			{
				serror = string.Format("mortgage.caculate_monthly_payment, error: {0} ", ex.Message);
			}

			return fPmt;
		}

		public static float CaculateHowMuchBorrow(float fsalary, float fdebt, float fprice, float frate, float fdown, float fterm, float ftax, float finsurance,
			out float fpayment_month, out float ftotal, out string serror)
		{
			float fHMB = 0.0f;

			fpayment_month = 0.0f;
			ftotal = 0.0f;
			serror = string.Empty;

			try
			{
				var fprincipal = fprice - fdown;
				var fperiod = fterm * 12;
				frate = frate * 0.01f / 12.0f;

				fpayment_month = (float)(fprincipal * frate / (1.0f - Math.Pow(1.0f + frate, -1 * fperiod)));
				ftotal = fpayment_month * fperiod;
				var fdebt1 = fdebt * 12 + ftax + finsurance + 0.0f;
				var fafford1 = ((1 - Math.Pow(1.0f + frate, -1 * fperiod)) * ((0.38f * fsalary) - fdebt1)) / (12 * frate);
				var fdebt2 = (ftax + finsurance) + 0.0f;
				var fafford2 = ((1 - Math.Pow(1.0f + frate, -1 * fperiod)) * ((0.33f * fsalary) - fdebt2)) / (12 * frate);

				fpayment_month = fpayment_month + ((ftax + finsurance) / 12);

				fHMB = (float)Math.Min(fafford1, fafford2);

				//function calculate() {
				// cF= document.forms["calculator"];

				//// var interest_30yr= 8.50;
				//// var interest_15yr= 8.00;

				// taxes= StripNonNumeric(cF.Taxes.value) *1;
				// insurance= StripNonNumeric(cF.Insurance.value) *1;

				// principal= StripNonNumeric(cF.Principal.value) - StripNonNumeric(cF.Down.value);
				// rate= (StripNonNumeric(cF.Interest.value) * .01) / 12.0;
				// period= StripNonNumeric(cF.Term.value) * 12;
				// payments= principal * rate / (1-Math.pow((1.0+rate),-period));
				// total= payments * period;
				// debt1= ( (StripNonNumeric(cF.Debt.value)*12) +taxes +insurance ) +0.0;
				// afford1= ((1-Math.pow((1.0+rate),-period)) * ((StripNonNumeric(cF.Salary.value) * .38) - debt1)) / (12*rate);
				// debt2= (taxes +insurance) +0.0;
				// afford2= ((1-Math.pow((1.0+rate),-period)) * ((StripNonNumeric(cF.Salary.value) * .33) - debt2)) / (12*rate);

				// affordStr= Math.min(afford1,afford2) +"";
				// affordStr= "(What you can afford: $" +affordStr.substring(0,affordStr.indexOf(".",0)) +")";
				// affordTD.innerHTML= affordStr;

				// totalStr= total +"";
				// totalStr.substring(0,totalStr.indexOf(".",0) +3);
				// totalStr= totalStr.substring(0,totalStr.indexOf(".",0) +3);

				// payments= payments +((taxes +insurance) / 12);
				// paymentStr= payments +"";
				// paymentStr= paymentStr.substring(0,paymentStr.indexOf(".",0)+3);

				// if (isNaN(payments)) {
				//  str= errorMessage;
				// } else {
				//  str= "<font color=red>Your monthly payment for the Purchase Price of $" +cF.Principal.value +
				//   " at " +cF.Interest.value +"%" +
				//   " for the term of " +cF.Term.value +
				//   " years is <b>$" +paymentStr +"</b>" +
				//   " for a Total P&I of <b>$" +totalStr +"</b></font>";
				// }
				// resultTD.innerHTML= str;



			}
			catch (Exception ex)
			{
				serror = string.Format("mortgage.caculate_how_much_borrow, error: {0} ", ex.Message);
			}

			return fHMB;
		}

		public static float CaculateWhatCanIAfford(float fpayment, float frate, float fterm, out string serror)
		{
			float fafford = 0.0f;
			serror = string.Empty;

			try
			{
				var frate1 = frate / (100 * 12.0f);
				var fterm1 = fterm * 12.0f;

				fafford = (float)(fpayment / (frate1 / (1.0f - Math.Pow(1.0f + frate1, -1 * fterm1))));

				//function calculate() {
				//cF= document.forms["calculator"];

				//var fStr,fPmt,fTerm,fRate,fLoan;
				//fPmt= ConvertToFloat(cF.Pmt.value);
				//fRate= ConvertToFloat(cF.Rate.value);
				//fTerm= ConvertToFloat(cF.Term.value);
				//fRate= fRate/(12.0*100); //Get monthly and convert to decimal equivalent
				//fTerm= fTerm * 12.0; //Convert to months

				//fLoan= fPmt / (fRate/(1.0 - Math.pow((1.0+fRate),-fTerm)));

				//if (isNaN(fPmt)) {
				// str= errorMessage;
				//} else {
				// fStr= fLoan +'';
				// if (fStr.indexOf(".")<0) fStr= fStr +".";
				// fStr= fStr +"00";
				// fStr= fStr.substring(0,fStr.indexOf(".")+3);

				// str= "<font color=red>Your loan will be for the amount of <b>$" +fStr +"</b>" +
				//  " at " +cF.Rate.value +"%" +
				//  " for the term of " +cF.Term.value +
				//  " years with monthly payments of $" +fPmt +"</font>";
				//}
				//resultTD.innerHTML= str;
			}
			catch (Exception ex)
			{
				serror = string.Format("mortgage.caculate_what_afford, error: {0} ", ex.Message);
			}

			return fafford;
		}

	}
}
